(function() {
    "use strict";

    /**
       * @ngdoc directive
       * @name webApp.directive:orderlist
       * @description
       * # orderlist 

      用法：
      <orderlist ng-model="chosedMember" set-member="setMember(member)"></orderlist>
      setMember格式：function(member){}
      
      */

    angular
        .module("webApp")
        //调用其他方法
        .run([
            "$templateCache",
            "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.orderlist.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive("orderlist", [
            "dataService",
            "$compile",
            "moment",
            function(dataService, $compile, moment) {
                return {
                    restrict: "E",
                    require: "ngModel",
                    replace: true,
                    scope: {
                        ngModel: "=",
                        stype: "@", //显示用户名字
                        setOrder: "&", //设置会员动作
                        xtype: "@" //搜索客户
                    },
                    template: "<span><a href='javascript:void(0);' ng-click='chose();' class='sui-btn btn-success'>{{'Order'|translate}}</span>",
                    controller: [
                        "$templateCache",
                        "$rootScope",
                        "$scope",
                        "$timeout",
                        function($templateCache, $rootScope, $scope, $timeout) {
                            $scope.items = [];
                            $scope.chose = function() {
                                $scope.pageData().then(showPop);
                            };

                            function showPop() {
                                $.alert({
                                    backdrop: "static",
                                    keyboard: true,
                                    title: '<form class="sui-form" style="margin:0px;"><span>{{"Order" | translate}}</span></form>',
                                    body: $templateCache.get("views/directives.orderlist.html"),
                                    okBtn: '{{"ok" | translate}}',
                                    closeBtn: true,
                                    transition: true,
                                    width: "large",
                                    height: "200",
                                    show: function(e) {
                                        $compile($(e.currentTarget).contents())($scope);
                                        return true;
                                    },
                                    okHide: function(e) {
                                        $timeout(function() {
                                            //console.log("选择的数据：", $scope.ngModel);
                                            if ($scope.setOrder) {
                                                $scope.setOrder({
                                                    order: $scope.ngModel
                                                });
                                            }
                                        });
                                    }
                                });
                            }

                            //请求数据
                            $scope.itemsTotal = 0;
                            $scope.pageSize = 5;
                            $scope.pageData = function(pageNo) {
                                var query = {
                                    page_no: pageNo ? pageNo : "1",
                                    page_size: $scope.pageSize
                                };
                                return dataService
                                    .orderGet(query, $scope.order_id)
                                    .then(function(list) {
                                        if (list) {
                                            $scope.orders = list.orders;
                                            $scope.ordersTotal = list.total_count;
                                        }
                                    });
                            };
                        }
                    ],
                    link: function(scope, element, attrs, ctrl) {
                        scope.$watch("ngModel", function(newValue, oldValue, scope) {
                            // console.log("ngModel:",newValue);
                            ctrl.$setViewValue(newValue);
                        });
                    }
                };
            }
        ])
        .run([
            "$templateCache",
            "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.pintuandetillist.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive("ptSchedule", [
            "dataService",
            "$compile",
            "moment",
            function(dataService, $compile, moment) {
                return {
                    restrict: "E",
                    require: "ngModel",
                    replace: true,
                    scope: {
                        ngModel: "="
                    },
                    template: '<div class="sui-progress progress-info progress-striped active"  ><div style="width: {{onewidth}}%;" class="bar" ng-click="infolist();">拼团:{{pintuan.current_customer_num}} / {{pintuan.need_customer_num}}</div>',
                    controller: [
                        "$templateCache",
                        "$rootScope",
                        "$scope",
                        "$timeout",
                        function($templateCache, $rootScope, $scope, $timeout) {
                            $scope.$on("event:up-store-id", function() {
                                $scope.init();
                            });

                            //初始化
                            $scope.init = function() {
                                if ($scope.ngModel) {
                                    $scope.pageData();
                                }
                            };
                            //拼团详情列表
                            $scope.infolist = function() {
                                $scope.detailData(1).then($scope.showPop);
                            };

                            $scope.pageData = function(pageNo) {
                                var data = {
                                    content: {
                                        pintuan_activity_id: $scope.ngModel,
                                        store_id: $rootScope.store_id
                                    }
                                };
                                return dataService
                                    .pintuanactivityGet(data)
                                    .then(function(list) {
                                        if (list.pintuanactivity_list) {
                                            if (list.pintuanactivity_list[0]) {
                                                $scope.pintuan = list.pintuanactivity_list[0];
                                                $scope.onewidth =
                                                    ($scope.pintuan.current_customer_num /
                                                        $scope.pintuan.need_customer_num) *
                                                    100;
                                            }
                                        }
                                    });
                            };

                            $scope.detailData = function() {
                                var data = {
                                    content: {
                                        pintuan_activity_id: $scope.ngModel
                                    }
                                };
                                return dataService.pintuandetilGet(data).then(function(list) {
                                    if (list.pintuan_detail_list) {
                                        $scope.detailDatas = list.pintuan_detail_list;
                                        $scope.total_count = list.total_count;
                                    }
                                });
                            };

                            $scope.showPop = function() {
                                $.alert({
                                    backdrop: "static",
                                    keyboard: true,
                                    title: '<form class="sui-form" style="margin:0px;"><span>{{"拼团详情" | translate}}</span></form>',
                                    body: $templateCache.get(
                                        "views/directives.pintuandetillist.html"
                                    ),
                                    okBtn: '{{"ok" | translate}}',
                                    closeBtn: true,
                                    transition: true,
                                    width: "large",
                                    height: "400",
                                    hasfoot: false,
                                    show: function(e) {
                                        $compile($(e.currentTarget).contents())($scope);
                                        return true;
                                    },
                                    okHide: function(e) {}
                                });
                            };
                            $scope.init();
                        }
                    ],
                    link: function(scope, element, attrs, ctrl) {}
                };
            }
        ])
        .directive("orderStatistics", [
            "dataService",
            "$compile",
            function(dataService, $compile) {
                return {
                    restrict: "E",
                    // replace: true,
                    scope: {},
                    templateUrl: "views/directives.orderstatistics.html",
                    controller: [
                        "$templateCache",
                        "$rootScope",
                        "$scope",
                        "$timeout",
                        "sessionService",
                        function(
                            $templateCache,
                            $rootScope,
                            $scope,
                            $timeout,
                            sessionService
                        ) {
                            $scope.$on("event:up-store-id", function() {
                                $scope.getdata();
                            });
                            $scope.getdata = function() {
                                var user = sessionService.get("tokenUser");

                                if (user && user.Role !== 4) {
                                    //今天
                                    var datetiem = moment().format("YYYY-MM-DD");
                                    //                         console.error("datetiem", datetiem);
                                    getorderList(
                                        datetiem,
                                        datetiem,
                                        "todayOrdersTotal",
                                        "todayOrderComplete",
                                        "todayOrderConfirm",
                                        1,
                                        1
                                    );
                                    getorderList(
                                        datetiem,
                                        datetiem,
                                        "todayOrdersTotalamount",
                                        "todayOrderCompleteamount",
                                        "todayOrderConfirmamount",
                                        null,
                                        1
                                    );
                                    //昨天
                                    var Lastdate = moment()
                                        .subtract(1, "days")
                                        .format("YYYY-MM-DD");
                                    getorderList(
                                        Lastdate,
                                        Lastdate,
                                        "LastOrdersTotal",
                                        "LastOrderComplete",
                                        "LastOrderConfirm",
                                        1
                                    );
                                    getorderList(
                                        Lastdate,
                                        Lastdate,
                                        "LastOrdersTotalamount",
                                        "LastOrderCompleteamount",
                                        "LastOrderConfirmamount"
                                    );
                                    //一周前
                                    var weekdate = moment()
                                        .subtract(7, "days")
                                        .format("YYYY-MM-DD");

                                    getorderList(
                                        weekdate,
                                        Lastdate,
                                        "WeekOrdersTotal",
                                        "WeekOrderComplete",
                                        "WeekOrderConfirm",
                                        1
                                    );
                                    getorderList(
                                        weekdate,
                                        Lastdate,
                                        "WeekOrdersTotalamount",
                                        "WeekOrderCompleteamount",
                                        "WeekOrderConfirmamount"
                                    );
                                    //一个月前
                                    var monthdate = moment()
                                        .subtract(30, "days")
                                        .format("YYYY-MM-DD");
                                    getorderList(
                                        monthdate,
                                        Lastdate,
                                        "MonthOrdersTotal",
                                        "MonthOrderComplete",
                                        "MonthOrderConfirm",
                                        1
                                    );
                                    getorderList(
                                        monthdate,
                                        Lastdate,
                                        "MonthOrdersTotalamount",
                                        "MonthOrderCompleteamount",
                                        "MonthOrderConfirmamount"
                                    );
                                }
                            };
                            $scope.getdata();
                            //获取列表
                            function getorderList(
                                sdate,
                                edate,
                                TotalData,
                                Complete,
                                Confirm,
                                type,
                                d
                            ) {
                                if (!$rootScope.store_id) {
                                    return false;
                                } else {
                                    var query = {
                                        content: {
                                            start_time: sdate + " 00:00:00",
                                            end_time: edate + " 23:59:59",
                                            store_id: $rootScope.store_id
                                        }
                                    };
                                    if (d) {
                                        query.content.end_time = moment().format(
                                            "YYYY-MM-DD HH:mm:ss"
                                        );
                                    }

                                    if (type) {
                                        // console.error("日期的时候", query.content);
                                        apiGetData(query, TotalData, "ordercountGet");
                                        var query1 = {
                                            content: {
                                                start_time: sdate + " 00:00:00",
                                                end_time: edate + " 23:59:59",
                                                store_id: $rootScope.store_id,
                                                status: "S_ORDER_COMPLETED"
                                            }
                                        };

                                        apiGetData(query1, Complete, "ordercountGet");
                                        var query2 = {
                                            content: {
                                                start_time: sdate + " 00:00:00",
                                                end_time: edate + " 23:59:59",
                                                store_id: $rootScope.store_id,
                                                status: "S_ORDER_WAIT_CONFIRM,S_ORDER_WAIT_DELIVERY,S_ORDER_WAIT_RECEIVE,S_ORDER_WAIT_RATE,S_ORDER_COMPLETED"
                                            }
                                        };

                                        apiGetData(query2, Confirm, "ordercountGet");
                                    } else {
                                        // console.error("金额点点滴滴", query.content);

                                        apiGetData(query, TotalData, "orderamountGet");
                                        var query1 = {
                                            content: {
                                                start_time: sdate + " 00:00:00",
                                                end_time: edate + " 23:59:59",
                                                store_id: $rootScope.store_id,
                                                status: "S_ORDER_COMPLETED"
                                            }
                                        };
                                        apiGetData(query1, Complete, "orderamountGet");
                                        var query2 = {
                                            content: {
                                                start_time: sdate + " 00:00:00",
                                                end_time: edate + " 23:59:59",
                                                store_id: $rootScope.store_id,
                                                status: "S_ORDER_WAIT_CONFIRM,S_ORDER_WAIT_DELIVERY,S_ORDER_WAIT_RECEIVE,S_ORDER_WAIT_RATE,S_ORDER_COMPLETED"
                                            }
                                        };
                                        apiGetData(query2, Confirm, "orderamountGet");
                                    }

                                    //  S_ORDER_COMPLETED S_ORDER_WAIT_CONFIRM
                                }
                            }

                            function apiGetData(query, is_title, api, st) {
                                var title = {
                                    LastOrdersTotal: "LastOrdersTotal",
                                    LastOrderComplete: "LastOrderComplete",
                                    LastOrderConfirm: "LastOrderConfirm",
                                    todayOrdersTotal: "todayOrdersTotal",
                                    todayOrderComplete: "todayOrderComplete",
                                    todayOrderConfirm: "todayOrderConfirm",
                                    WeekOrdersTotal: "WeekOrdersTotal",
                                    WeekOrderComplete: "WeekOrderComplete",
                                    WeekOrderConfirm: "WeekOrderConfirm",
                                    MonthOrdersTotal: "MonthOrdersTotal",
                                    MonthOrderComplete: "MonthOrderComplete",
                                    MonthOrderConfirm: "MonthOrderConfirm",
                                    LastOrdersTotalamount: "LastOrdersTotalamount",
                                    LastOrderCompleteamount: "LastOrderCompleteamount",
                                    LastOrderConfirmamount: "LastOrderConfirmamount",
                                    todayOrdersTotalamount: "todayOrdersTotalamount",
                                    todayOrderCompleteamount: "todayOrderComplete",
                                    todayOrderConfirmamount: "todayOrderConfirmamount",
                                    WeekOrdersTotalamount: "WeekOrdersTotalamount",
                                    WeekOrderCompleteamount: "WeekOrderCompleteamount",
                                    WeekOrderConfirmamount: "WeekOrderConfirmamount",
                                    MonthOrdersTotalamount: "MonthOrdersTotalamount",
                                    MonthOrderCompleteamount: "MonthOrderCompleteamount",
                                    MonthOrderConfirmamount: "MonthOrderConfirmamount"
                                };
                                var apiRay = {
                                    ordercountGet: "ordercountGet",
                                    orderamountGet: "orderamountGet"
                                };

                                // console.error("111111", query);
                                $scope[title[is_title]] = 0;
                                $timeout(function() {
                                    dataService[apiRay[api]](query).then(function(list) {
                                        if (list.count) {
                                            $scope[title[is_title]] = list.count;
                                        }
                                        if (list.sum) {
                                            $scope[title[is_title]] = list.sum;
                                        }
                                        //                                 console.error($scope[title[is_title]]);
                                    });
                                });
                            }
                        }
                    ],
                    link: function(scope, element, attrs, ctrl) {}
                };
            }
        ])
        .run([
            "$templateCache",
            "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.orderdeliveraddress.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive("orderDeliverAddress", [
            "dataService",
            "$compile",
            function(dataService, $compile) {
                return {
                    restrict: "E",
                    require: "ngModel",
                    replace: true,
                    scope: {
                        ngModel: "=", //具体地址
                        backcall: "&", //回调方法
                        order: "=" //订单数据
                    },
                    template: '<div ><button  class="sui-btn btn-success" ng-click="initData()">修改收货信息</button></div>',
                    controller: [
                        "$templateCache",
                        "$rootScope",
                        "$scope",
                        "$timeout",
                        "sessionService",
                        function(
                            $templateCache,
                            $rootScope,
                            $scope,
                            $timeout,
                            sessionService
                        ) {
                            $scope.getorder = function() {
                                if ($scope.order) {
                                    return dataService.resolve($scope.order);
                                }
                                return dataService.orderOne($scope.ngModel).then(function(one) {
                                    if (one) {
                                        $scope.oldValue = clone(one);
                                        return ($scope.order = one);
                                    }
                                });
                            };

                            $scope.initData = function() {
                                $scope.getorder().then($scope.showPop);
                            };
                            $scope.saveupdate = function(data) {
                                $scope.newValue = data;
                            };
                            $scope.showPop = function() {
                                $.alert({
                                    backdrop: "static",
                                    keyboard: true,
                                    title: '<form class="sui-form" style="margin:0px;">修改收货信息</span></form>',
                                    body: $templateCache.get(
                                        "views/directives.orderdeliveraddress.html"
                                    ),
                                    okBtn: '{{"ok" | translate}}',
                                    closeBtn: true,
                                    transition: true,
                                    width: "small",
                                    // height: '400',
                                    // hasfoot: false,
                                    show: function(e) {
                                        $compile($(e.currentTarget).contents())($scope);
                                        return true;
                                    },
                                    okHide: function(e) {
                                        if ($scope.backcall) {
                                            if (!$scope.newValue) {
                                                $scope.newValue = {};
                                            }
                                            if ($scope.order.deliver_address !== $scope.oldValue.deliver_address) {
                                                $scope.newValue.deliver_address = $scope.order.deliver_address;
                                            }
                                            if ($scope.order.deliver_phone !== $scope.oldValue.deliver_phone) {
                                                $scope.newValue.deliver_phone = $scope.order.deliver_phone;
                                            }
                                            if ($scope.order.deliver_name !== $scope.oldValue.deliver_name) {
                                                $scope.newValue.deliver_name = $scope.order.deliver_name;
                                            }
                                            $scope.backcall({
                                                data: $scope.newValue
                                            });
                                        }
                                    }
                                });
                            };
                        }
                    ],
                    link: function(scope, element, attrs, ctrl) {}
                };
            }
        ])
        .run([
            "$templateCache",
            "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.wechatlogistic.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive("wxLogistic", [
            "dataService",
            "$compile",
            function(dataService, $compile) {
                return {
                    restrict: "E",
                    // require: "",
                    replace: true,
                    scope: {
                        // ngModel: "=", //具体地址
                        orderId: "=",
                        backcall: "&" //回调方法
                    },
                    template: '<div ><button  class="sui-btn btn-success" ng-click="showPop()">使用微信物流助手发货</button></div>',
                    controller: [
                        "$templateCache",
                        "$rootScope",
                        "$scope",
                        "$timeout",
                        "sessionService",
                        "helper",
                        function(
                            $templateCache,
                            $rootScope,
                            $scope,
                            $timeout,
                            sessionService,
                            helper
                        ) {

                            $scope.wxappBack = function(data) {
                                $scope.lists = [];
                                dataService.deliveryGet({
                                    wxappid: data
                                }).then(function(list) {
                                    if (list.data) {
                                        $scope.lists = list.data;
                                    } else {
                                        helper.myAlert("error:" + list.errmsg);
                                    }
                                });
                            }

                            //获取 快递配置数据 列表
                            $scope.deliveryget = function(deliveryid) {
                                var data = {
                                    store_id: $rootScope.store_id,
                                    delivery_id: deliveryid,
                                    wxapp_id: $scope.wxappid
                                };
                                return dataService.deliveryConfigGet(data).then(function(list) {
                                    if (list.data) {
                                        return list.data;
                                    } else {
                                        return false;
                                    }
                                });
                            };
                            //添加发货
                            $scope.businessorderadd = function(deliveryid) {
                                $scope.deliveryget(deliveryid).then(function(data) {
                                    if (data) {
                                        dataService.businessOrderAdd({
                                            order_id: $scope.orderId
                                        }).then(function(list) {
                                            if (list.data) {
                                                $scope.lists = list.data;
                                            } else {
                                                helper.myAlert("error:" + list.errmsg);
                                            }
                                        });
                                    } else {
                                        helper.message("请先配置快递账号信息", "error");
                                        return false;
                                    }
                                })

                            }
                            $scope.showPop = function() {
                                $.alert({
                                    backdrop: "static",
                                    keyboard: true,
                                    title: '微信助手发货',
                                    body: $templateCache.get(
                                        "views/directives.wechatlogistic.html"
                                    ),
                                    closeBtn: true,
                                    transition: true,
                                    width: "large",
                                    height: 400,
                                    hasfoot: false,
                                    show: function(e) {
                                        $compile($(e.currentTarget).contents())($scope);
                                        return true;
                                    },
                                    okHide: function(e) {

                                    }
                                });
                            };
                            //获取 快递配置数据 列表
                            $scope.delivery = function() {
                                var data = {
                                    store_id: $rootScope.store_id,
                                    delivery_id: $scope.deliveryid,
                                    wxapp_id: $scope.wxappid
                                };
                                return dataService.deliveryConfigGet(data).then(function(list) {
                                    if (list.data) {
                                        $scope.oldata = list.data;
                                        return $scope.data = clone($scope.oldata);
                                    } else {
                                        helper.myAlert("没有配置快递账号", "error");
                                        return false;
                                    }
                                });
                            };
                        }
                    ],
                    link: function(scope, element, attrs, ctrl) {}
                };
            }
        ])
        .run([
            "$templateCache",
            "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.wechatlogisticconfig.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive("wxLogisticConfig", [
            "dataService",
            "$compile",
            function(dataService, $compile) {
                return {
                    restrict: "E",
                    replace: true,
                    scope: {
                        wxappid: "=",
                        deliveryid: "=",
                        deliveryname: "=",
                    },
                    template: '<div ><button  class="sui-btn btn-success" ng-click="initData()">账号配置</button></div>',
                    controller: [
                        "$templateCache",
                        "$rootScope",
                        "$scope",
                        "$timeout",
                        "sessionService",
                        "helper",
                        function(
                            $templateCache,
                            $rootScope,
                            $scope,
                            $timeout,
                            sessionService,
                            helper
                        ) {
                            //获取 快递配置数据 列表
                            $scope.deliveryget = function() {
                                var data = {
                                    store_id: $rootScope.store_id,
                                    delivery_id: $scope.deliveryid,
                                    wxapp_id: $scope.wxappid
                                };
                                return dataService.deliveryConfigGet(data).then(function(list) {
                                    if (list.data) {
                                        $scope.oldata = list.data;
                                        return $scope.data = clone($scope.oldata);
                                    }
                                });
                            };

                            $scope.initData = function() {
                                $scope.deliveryget().then($scope.showPop);
                            };

                            $scope.saveupdate = function() {
                                if (!$scope.data || !$scope.data.biz_id) {
                                    helper.message("请输入快递编码");
                                    return false;
                                }
                                var data = {
                                    store_id: $rootScope.store_id,
                                    delivery_id: $scope.deliveryid,
                                    wxapp_id: $scope.wxappid,
                                    biz_id: $scope.data.biz_id,
                                    delivery_name: $scope.deliveryname
                                };
                                if ($scope.data.wx_logistics_id) {
                                    data.wx_logistics_id = $scope.data.wx_logistics_id;
                                    if ($scope.data.biz_id === $scope.oldata.biz_id) {
                                        helper.message("保存成功");
                                        return false;
                                    }
                                }
                                console.error("data", data, $scope.data);
                                dataService.deliveryConfigAdd(data).then(function(list) {
                                    if (list.id) {
                                        helper.message("保存成功");
                                    } else {
                                        helper.message("保存失败");
                                    }
                                });
                            }
                            $scope.showPop = function() {
                                $.alert({
                                    backdrop: "static",
                                    keyboard: true,
                                    title: '<form class="sui-form" style="margin:0px;">{{deliveryname}}快递账号设置</span></form>',
                                    body: $templateCache.get(
                                        "views/directives.wechatlogisticconfig.html"
                                    ),
                                    okBtn: '{{"ok" | translate}}',
                                    closeBtn: true,
                                    transition: true,
                                    width: "normal",
                                    show: function(e) {
                                        $compile($(e.currentTarget).contents())($scope);
                                        return true;
                                    },
                                    okHide: function(e) {
                                        $scope.saveupdate();
                                    }
                                });
                            };
                        }
                    ],
                    link: function(scope, element, attrs, ctrl) {}
                };
            }
        ])
        .run([
            "$templateCache",
            "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.wechatlogisticdelivery.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive("wxLogisticAdd", [
            "dataService",
            "$compile",
            function(dataService, $compile) {
                return {
                    restrict: "E",
                    replace: true,
                    scope: {
                        wxappid: "=",
                        orderId: "=",
                        deliveryid: "=",
                        deliveryname: "="
                    },
                    template: '<div ><button  class="sui-btn btn-primary" ng-click="initData()">使用此物流发货</button></div>',
                    controller: [
                        "$templateCache",
                        "$rootScope",
                        "$scope",
                        "$timeout",
                        "sessionService",
                        "helper",
                        function(
                            $templateCache,
                            $rootScope,
                            $scope,
                            $timeout,
                            sessionService,
                            helper
                        ) {
                            //获取 快递配置数据 列表
                            $scope.deliveryget = function() {
                                var data = {
                                    store_id: $rootScope.store_id,
                                    delivery_id: $scope.deliveryid,
                                    wxapp_id: $scope.wxappid
                                };
                                return dataService.deliveryConfigGet(data).then(function(list) {
                                    if (list.data) {
                                        return list.data;
                                    } else {
                                        return false;
                                    }
                                });
                            };

                            $scope.initData = function() {
                                $scope.deliveryget().then(function(data) {
                                    if (data) {
                                        $scope.wxservicetype();
                                        $scope.showPop();
                                    } else {
                                        helper.message("请先配置快递账号信息", "error");
                                        return false;
                                    }
                                })
                            };
                            $scope.wxservicetype = function() {
                                var data = {
                                    delivery_id: $scope.deliveryid
                                };
                                return dataService.wxservicetype(data).then(function(list) {
                                    if (list) {
                                        console.error(list);
                                        $scope.services = [];
                                        _.map(list, function(da, ke) {
                                            var d = {
                                                service_type: ke,
                                                service_name: da
                                            };
                                            $scope.services.push(d);
                                            $scope.service = d;
                                        });
                                    }
                                });
                            }
                            $scope.saveupdate = function() {
                                console.error("dd", $scope.data);
                                $scope.data.service_type = $scope.service.service_type;
                                $scope.data.service_name = $scope.service.service_name;
                                dataService.businessOrderAdd({
                                    order_id: $scope.orderId,
                                    data: $scope.data,
                                    delivery_id: $scope.deliveryid
                                }).then(function(list) {
                                    if (list) {
                                        //后续修改
                                    } else {
                                        helper.myAlert("error:" + list.Msg);
                                    }
                                });
                            }
                            $scope.showPop = function() {
                                $scope.data = {
                                    count: 1,
                                    weight: 1,
                                    space_x: 1,
                                    space_y: 1,
                                    space_z: 1
                                }
                                $.alert({
                                    backdrop: "static",
                                    keyboard: true,
                                    title: '{{deliveryname}}快递发货设置',
                                    body: $templateCache.get(
                                        "views/directives.wechatlogisticdelivery.html"
                                    ),
                                    closeBtn: true,
                                    transition: true,
                                    width: "normal",
                                    hasfoot: false,
                                    show: function(e) {
                                        $compile($(e.currentTarget).contents())($scope);
                                        return true;
                                    },
                                    okHide: function(e) {
                                        $scope.saveupdate();
                                    }
                                });
                            };
                        }
                    ],
                    link: function(scope, element, attrs, ctrl) {}
                };
            }
        ])

    .directive("paymentList", [
            "dataService",
            "$compile",
            function(dataService, $compile) {
                return {
                    restrict: "E",
                    // replace: true,
                    scope: {
                        status: "="
                    },
                    templateUrl: "views/directives.paymentlist.html",
                    controller: [
                        "$templateCache",
                        "$rootScope",
                        "$scope",
                        "$timeout",
                        "sessionService",
                        "$filter",
                        function(
                            $templateCache,
                            $rootScope,
                            $scope,
                            $timeout,
                            sessionService,
                            $filter
                        ) {
                            $scope.$on("event:up-store-id", function() {
                                $scope.getdata();
                            });

                            $scope.store_id = $rootScope.store_id;
                            $scope.chartConfig = {};
                            $scope.chartConfig1 = {};
                            $scope.chartConfig2 = {};
                            $scope.chartConfig3 = {};
                            $scope.getStorelist = function() {
                                $scope.user = sessionService.get("tokenUser");
                                if ($scope.user && $scope.user.Role !== 4 && $scope.status) {
                                    dataService.storeGet({
                                        page_size: 100
                                    }).then(function(list) {
                                        if (list.store) {
                                            var datas;
                                            async.map(
                                                list.store,
                                                function(data, callback) {
                                                    if (data) {
                                                        dataService
                                                            .storetongjiPaymentGet(
                                                                $scope.datetiem,
                                                                data.store_id
                                                            )
                                                            .then(function(r) {
                                                                // console.error("r", r);
                                                                if (r) {
                                                                    if (datas) {
                                                                        _.map(r, function(da, key) {
                                                                            _.map(da, function(sub, subkey) {
                                                                                if (subkey != "date") {
                                                                                    if (subkey.search("money") != -1) {
                                                                                        datas[key][subkey] = parseFloat(datas[key][subkey] + sub).toFixed(2);
                                                                                    } else {
                                                                                        datas[key][subkey] = datas[key][subkey] + sub;
                                                                                    }

                                                                                    // console.error("subkey",sub);
                                                                                    // console.error("datas[key][subkey]",subkey);
                                                                                    // console.error("datas[key][subkey]",datas[key][subkey]);
                                                                                }
                                                                            });
                                                                        });
                                                                    } else {
                                                                        datas = clone(r);
                                                                    }
                                                                    if (data.store_id == $scope.store_id) {
                                                                        $scope.paymentLisone = r;
                                                                    }
                                                                    callback(null);
                                                                } else {
                                                                    callback(null);
                                                                }
                                                            });
                                                    }
                                                },
                                                function(e, r) {
                                                    $scope.paymentList = [];
                                                    $scope.paymentList.push(datas);
                                                    $scope.paymentList.push($scope.paymentLisone);
                                                    // console.error("$scope.paymentList", $scope.paymentList, $scope.paymentLisone);
                                                }
                                            );
                                        }
                                    });
                                }
                            };

                            $scope.getdata = function() {
                                $scope.getStorelist();
                                var user = sessionService.get("tokenUser");
                                if (user && user.Role !== 4 && $scope.status) {
                                    //今天
                                    var datetiem = moment().format("YYYY-MM-DD");
                                    $scope.datetiem = toStdTime(datetiem);
                                    paymentGetData();
                                    // WxappanalysiGetData();
                                }
                            };
                            $scope.getdata();
                            //把当地变成标准时间
                            function toStdTime(x) {
                                return $filter("stdTime")(x);
                            }
                            $scope.wxaacallback = function(app_id) {
                                WxappanalysiGetData(app_id);
                            };

                            function paymentGetData() {
                                dataService.storetongjiPaymentGet($scope.datetiem).then(function(list) {
                                    if (list) {
                                        $scope.list = list;
                                        list.Yesterday = list.Yesterday ? list.Yesterday : {};
                                        list.Today = list.Today ? list.Today : {};
                                        //所有店铺支付金额数据
                                        $scope.chartConfig = {
                                            chart: {
                                                width: 300,
                                                height: 300,
                                                spacing: [40, 0, 40, 0]
                                            },
                                            title: {
                                                text: '所有店铺支付金额数据',
                                                style: {
                                                    fontSize: 16
                                                },
                                                align: "left"
                                            },
                                            credits: {
                                                enabled: false
                                            },
                                            tooltip: {
                                                pointFormat: '总支付金额:{point.y}元</b>'
                                            },
                                            plotOptions: {
                                                pie: {
                                                    allowPointSelect: true,
                                                    cursor: 'pointer',
                                                    dataLabels: {
                                                        enabled: true,
                                                        format: '{point.y}',
                                                        style: {
                                                            color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                                                        }
                                                    },
                                                }
                                            },
                                            series: [{
                                                type: 'pie',
                                                innerSize: '70%',
                                                data: [{
                                                        name: '今日',
                                                        color: '#1E90FF',
                                                        y: list.Today ? list.Today.money : 0
                                                    },
                                                    {
                                                        name: '昨日',
                                                        color: '#ABADC6',
                                                        y: list.Yesterday ? list.Yesterday.money : 0
                                                    }
                                                ]
                                            }]
                                        };
                                        //所有店铺支付笔数数据
                                        $scope.chartConfig1 = {
                                            chart: {
                                                width: 300,
                                                height: 300,
                                                spacing: [40, 0, 40, 0]
                                            },
                                            title: {
                                                text: '所有店铺支付笔数数据',
                                                style: {
                                                    fontSize: 16
                                                },
                                                align: "left"
                                            },
                                            credits: {
                                                enabled: false
                                            },
                                            tooltip: {
                                                pointFormat: '总支付笔数:{point.y}笔</b>'
                                            },
                                            plotOptions: {
                                                pie: {
                                                    allowPointSelect: true,
                                                    cursor: 'pointer',
                                                    dataLabels: {
                                                        enabled: true,
                                                        format: '{point.y}',
                                                        style: {
                                                            color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                                                        }
                                                    },
                                                }
                                            },
                                            series: [{
                                                type: 'pie',
                                                innerSize: '70%',
                                                data: [{
                                                        name: '今日',
                                                        color: '#ED4A4B',
                                                        y: list.Today ? list.Today.num : 0
                                                    },
                                                    {
                                                        name: '昨日',
                                                        color: '#ABADC6',
                                                        y: list.Yesterday ? list.Yesterday.num : 0
                                                    }
                                                ]
                                            }]
                                        };
                                        //今日昨日支付金额
                                        $scope.chartConfig2 = {
                                            chart: {
                                                type: 'column',
                                                width: 800,
                                                height: 300,
                                                spacing: [40, 0, 40, 0]
                                            },
                                            title: {
                                                text: '支付金额'
                                            },
                                            xAxis: {
                                                categories: [
                                                    '余额', '充值', '现金', '面付', '预约', '签到', '报名', '退款', '普通订单', '酒店服务'
                                                ],
                                                crosshair: true
                                            },
                                            yAxis: {
                                                min: 0,
                                                title: {
                                                    text: '支付金额 (元)'
                                                }
                                            },
                                            credits: {
                                                enabled: false
                                            },
                                            tooltip: {
                                                headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
                                                pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
                                                    '<td style="padding:0"><b>{point.y:.1f} 元</b></td></tr>',
                                                footerFormat: '</table>',
                                                shared: true,
                                                useHTML: true
                                            },
                                            plotOptions: {
                                                column: {
                                                    borderWidth: 0
                                                }
                                            },
                                            series: [{
                                                name: '昨日',
                                                color: '#1E90FF',
                                                data: [list.Yesterday.balance_money, list.Yesterday.chongzhi_money, list.Yesterday.cash_money, list.Yesterday.mianfu_money, list.Yesterday.reserve_money, list.Yesterday.check_in_money, list.Yesterday.register_money, list.Yesterday.tuikuan_money, list.Yesterday.order_money, list.Yesterday.hotel_reserve_money]
                                            }, {
                                                name: '今日',
                                                color: '#ABADC6',
                                                data: [list.Today.balance_money, list.Today.chongzhi_money, list.Today.cash_money, list.Today.mianfu_money, list.Today.reserve_money, list.Today.check_in_money, list.Today.register_money, list.Today.tuikuan_money, list.Today.order_money, list.Today.hotel_reserve_money]
                                            }]
                                        };
                                        //今日昨日支付笔数
                                        $scope.chartConfig3 = {
                                            chart: {
                                                type: 'column',
                                                width: 800,
                                                height: 300,
                                                spacing: [40, 0, 40, 0]
                                            },
                                            title: {
                                                text: '支付笔数'
                                            },
                                            xAxis: {
                                                categories: [
                                                    '余额', '充值', '现金', '面付', '预约', '签到', '报名', '退款', '普通订单', '酒店服务'
                                                ],
                                                crosshair: true
                                            },
                                            yAxis: {
                                                min: 0,
                                                title: {
                                                    text: '支付笔数 (笔)'
                                                }
                                            },
                                            credits: {
                                                enabled: false
                                            },
                                            tooltip: {
                                                headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
                                                pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
                                                    '<td style="padding:0"><b>{point.y:.1f} 笔</b></td></tr>',
                                                footerFormat: '</table>',
                                                shared: true,
                                                useHTML: true
                                            },
                                            plotOptions: {
                                                column: {
                                                    borderWidth: 0
                                                }
                                            },
                                            series: [{
                                                name: '昨日',
                                                color: '#ED4A4B',
                                                data: [list.Yesterday.balance_num, list.Yesterday.chongzhi_num, list.Yesterday.cash_num, list.Yesterday.mianfu_num, list.Yesterday.reserve_num, list.Yesterday.check_in_num, list.Yesterday.register_num, list.Yesterday.tuikuan_num, list.Yesterday.order_num, list.Yesterday.hotel_reserve_num]
                                            }, {
                                                name: '今日',
                                                color: '#ABADC6',
                                                data: [list.Today.balance_num, list.Today.chongzhi_num, list.Today.cash_num, list.Today.mianfu_num, list.Today.reserve_num, list.Today.check_in_num, list.Today.register_num, list.Today.tuikuan_num, list.Today.order_num, list.Today.hotel_reserve_num]
                                            }]
                                        };
                                    }
                                });
                            }

                            function WxappanalysiGetData(app_id) {
                                dataService
                                    .storetongjiWxappanalysiGet(app_id)
                                    .then(function(list) {
                                        if (list) {
                                            $scope.wxapplist = list;
                                        }
                                    });
                            }
                        }
                    ],
                    link: function(scope, element, attrs, ctrl) {}
                };
            }
        ])
        .directive("orderRefund", [
            "dataService",
            "$compile",
            "moment",
            function(dataService, $compile, moment) {
                return {
                    restrict: "E",
                    replace: true,
                    scope: {
                        orderId: "=",
                        pickpointId: "=",
                    },
                    template: '<span ng-show="show"><button ng-click="Refund($event)" class="sui-btn btn-success">海关退单</button></span>',
                    controller: [
                        "$templateCache",
                        "$rootScope",
                        "$scope",
                        "$timeout",
                        "helper",
                        function($templateCache, $rootScope, $scope, $timeout, helper) {
                            $scope.Refund = function($event) {
                                helper.myConfirm('您确认执行海关退单吗？', function() {
                                    dataService.postOrder({
                                        'order_id': $scope.orderId,
                                        "type": "5"
                                    }).then(function(one) {
                                        helper.myTip(one.Msg, $event.currentTarget);
                                    });
                                });
                            }

                            //请求数据
                            $scope.init = function() {
                                var query = {
                                    pickpoint_id: $scope.pickpointId,
                                    store_id: $rootScope.store_id
                                };
                                dataService.wmsGet(query, 24 * 3600).then(function(list) {
                                    if (list && list.count >= 1) {
                                        if (list.type === 'Wmswh') {
                                            $scope.show = 1;
                                        }
                                    }
                                });
                            };
                            $scope.init();
                        }
                    ],
                    link: function(scope, element, attrs, ctrl) {

                    }
                };
            }
        ])
        .directive("detailedList", [
            "dataService",
            "$compile",
            "moment",
            function(dataService, $compile, moment) {
                return {
                    restrict: "E",
                    replace: true,
                    scope: {
                        orderId: "=",
                        pickpointId: "=",
                        type: "@",
                    },
                    template: '<span ng-show="show"><button ng-show="type ==4"  ng-click="post($event)" class="sui-btn btn-success">推送清单</button><button ng-show="type ==6"  ng-click="post($event)" class="sui-btn btn-success">推送运单</button></span>',
                    controller: [
                        "$templateCache",
                        "$rootScope",
                        "$scope",
                        "$timeout",
                        "helper",
                        function($templateCache, $rootScope, $scope, $timeout, helper) {
                            $scope.post = function($event) {
                                dataService.postOrder($scope.orderId, $scope.type).then(function(one) {
                                    helper.myTip(one.Msg, $event.currentTarget);
                                });
                            }

                            //请求数据
                            $scope.init = function() {
                                var query = {
                                    pickpoint_id: $scope.pickpointId,
                                    store_id: $rootScope.store_id
                                };
                                dataService.wmsGet(query, 24 * 3600).then(function(list) {
                                    if (list && list.count >= 1) {
                                        if (list.type === 'WmsSongDao') {
                                            $scope.show = 1;
                                        }
                                    }
                                });
                            };
                            $scope.init();
                        }
                    ],
                    link: function(scope, element, attrs, ctrl) {

                    }
                };
            }
        ])
        .directive("orderHgStatus", [
            "dataService",
            "$compile",
            "moment",
            function(dataService, $compile, moment) {
                return {
                    restrict: "E",
                    replace: true,
                    scope: {
                        orderId: "=",
                    },
                    template: '<div ng-show="show"><div class="sui-msg msg-success"><div class="msg-con">{{show.time|ddtt}}：{{show.state}}</div><s class="msg-icon"></s></div></div>',
                    controller: [
                        "$templateCache",
                        "$rootScope",
                        "$scope",
                        "$timeout",
                        "helper",
                        function($templateCache, $rootScope, $scope, $timeout, helper) {
                            //请求数据
                            $scope.init = function() {
                                var query = {
                                    order_id: $scope.orderId,
                                    one: 1
                                };
                                dataService.getCustomsnotifys(query).then(function(result) {
                                    if (result.order_data && result.order_data[0]) {
                                        $scope.show = result.order_data[0];
                                    }
                                });
                            };
                            $scope.init();
                        }
                    ],
                    link: function(scope, element, attrs, ctrl) {

                    }
                };
            }
        ])
        .directive("orderDdStatus", [
            "dataService",
            "$compile",
            "moment",
            function(dataService, $compile, moment) {
                return {
                    restrict: "E",
                    replace: true,
                    scope: {
                        ngModel: "=",
                    },
                    template: '<div ng-show="show"><div class="sui-msg msg-info"> <order-status ng-model="show.status" stype="1"></order-status></div><s class="msg-icon"></s> </div>',
                    controller: [
                        "$templateCache",
                        "$rootScope",
                        "$scope",
                        "$timeout",
                        "helper",
                        function($templateCache, $rootScope, $scope, $timeout, helper) {
                            //请求数据
                            $scope.init = function() {
                                dataService.orderOne($scope.ngModel).then(function(result) {
                                    if (result) {
                                        $scope.show = result;
                                    }
                                });
                            };
                            $scope.init();
                        }
                    ],
                    link: function(scope, element, attrs, ctrl) {

                    }
                };
            }
        ])
        .run([
            "$templateCache",
            "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.orderdelivergoods.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive("orderDeliverGoods", [
            "dataService",
            "$compile",
            "moment",
            "ERP_CONFIG",
            "helper",
            function(dataService, $compile, moment, ERP_CONFIG, helper) {
                return {
                    restrict: "E",
                    // require: 'ngModel',
                    replace: true,
                    scope: {
                        // ngModel: '=',
                        backcall: "&"
                    },
                    template: '<button  class="sui-btn btn-success" ng-click="showPop()" >{{title}}</button>',
                    controller: [
                        "$templateCache",
                        "$rootScope",
                        "$scope",
                        "$timeout",
                        function($templateCache, $rootScope, $scope, $timeout) {
                            $scope.title = "批量发货";
                            $scope.wl = ERP_CONFIG.webUrl + "/web/dhfshop/app/images/wl.csv";
                            $scope.showPop = function() {
                                $.alert({
                                    backdrop: "static",
                                    keyboard: true,
                                    title: "{{title}}",
                                    body: $templateCache.get(
                                        "views/directives.orderdelivergoods.html"
                                    ),
                                    okBtn: '{{"ok" | translate}}',
                                    closeBtn: true,
                                    transition: true,
                                    width: "normal",
                                    height: "400",
                                    hasfoot: false,
                                    show: function(e) {
                                        $compile($(e.currentTarget).contents())($scope);
                                        return true;
                                    },
                                    okHide: function(e) {
                                        // $scope.postData();
                                    }
                                });
                            };
                            $scope.postData = function($event) {
                                helper.myTip("操作中！", $event.currentTarget);
                                dataService
                                    .batchDeliverGoods({
                                        data: $scope.item
                                    })
                                    .then(function(list) {
                                        if (list) {
                                            $scope.lists = list.Msg;
                                            if ($scope.backcall) {
                                                $scope.backcall();
                                            }
                                        }
                                    });
                            };
                        }
                    ],
                    link: function(scope, element, attrs, ctrl) {}
                };
            }
        ]);
})();